namespace Trader.Trading
{
    using System;
    using System.Collections.Generic;
    using BO.Interface;
    using Interface;

    public class CommissionCharger : ICommissionCharger
    {
        private readonly decimal commission;

        private readonly Dictionary<DateTime, IMoney> datesCommissions;

        public CommissionCharger(decimal commission)
        {
            this.commission = commission/100M;
            datesCommissions = new Dictionary<DateTime, IMoney>();
        }

        #region ICommissionCharger Members

        public decimal Commission
        {
            get { return commission*100; }
        }

        public IMoney Charge(ITradeAction tradeAction, DateTime date)
        {
            IMoney actionCommission = commission*tradeAction.Stock.AsMoney(tradeAction.Units, tradeAction.Date);

            if (!datesCommissions.ContainsKey(date))
            {
                datesCommissions.Add(date, actionCommission);
            }
            else
            {
                datesCommissions[date].Add(actionCommission, date);
            }

            return actionCommission;
        }

        public IMoney this[DateTime date]
        {
            get { return datesCommissions.ContainsKey(date) ? datesCommissions[date] : null; }
        }

        #endregion
    }
}